package com.shop2cn.iapi.agentproducts.testcase;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.LoadPicsBean;
import com.shop2cn.iapi.agentproducts.parameter.ModifyAgentProductBean;
import com.shop2cn.iapi.agentproducts.service.LoadPicsCall;
import com.shop2cn.iapi.agentproducts.service.ModifyAgentProductCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AgentProductCallService;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.FontImage;
import com.ymttest.utils.TestDataManager;
/**
*下载图片
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_LoadPics{
	private static LoadPicsBean loadpicsBean;
	private static LoadPicsCall loadpicsCall;
	//渠道商
	private static int sellerId = AgentUsers.rootUserId;
	private static String sellerName = "NickautoTest";
	//渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	//渠道商->代理商2
	private static int agentUserId2 = AgentUsers.agentUserId2;
	private static String agentUserName2 = "NickautoTest";
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("下载图片");
	}

	@Before
	public void caseUp() {
		loadpicsBean=new LoadPicsBean();
		loadpicsCall=new LoadPicsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(loadpicsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_LoadPics_01() {
		Logger.start(true,"下载图片");//业务校验
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,sellerName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			loadpicsBean.setCsId(0);
			loadpicsBean.setProductId(productId);
			loadpicsBean.setUserId(sellerId);
			loadpicsCall.setData(loadpicsBean);
			loadpicsCall.callService();			
			Logger.verifyEquals("201",loadpicsCall.getString("code"),"验证code");
			Logger.verifyEquals("商品二维码加载中，无法完成下载，请稍后再试。",loadpicsCall.getString("message"),"验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_LoadPics_02() {
		Logger.start(true, "继承上级，编辑商品后，二维码会刷新");
		try {
			 ModifyAgentProductBean modifyagentproductBean = new ModifyAgentProductBean();
			ModifyAgentProductCall modifyagentproductCall = new ModifyAgentProductCall();
			// 创建一个渠道商，一个代理商，一个客服
			UserCreateBean usercreateBean1 = DGUserCallService.initUsercreateBean();
			int userId_channl = DGUserCallService.UserCreateCall(usercreateBean1);
			usercreateBean1 = DGUserCallService.initUsercreateBean();
			int userId_agent = DGUserCallService.UserCreateCall(usercreateBean1);
			// 设置渠道商
			createmchBean createmchbean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			// 渠道商创建一个邀请码，出来一个自动继承的代理商、自动继承渠道商刚才的商品
			CreateCodeBean createbean = GodmchManageCallService.InitcreateCodeBeanForAgent(userId_channl);
			createbean.setAgentModel(1);// 自动继承
			CreateCodeCall CreateCodeCall = GodmchManageCallService.createCodeCall(createbean);
			List<createmchBean> createmchList = GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl,
					userId_agent); //
			createmchList.get(0).setCode(CreateCodeCall.getCode());
			GodmchManageCallService
					.createmchCallForAgent(createmchList.toArray(new createmchBean[createmchList.size()]));
			Thread.sleep(5000);
			
//			自动继承id--：500275829  500275830
//			自助选货id--自己有水印，代理有水印：500275831  500275832
//			自助选货id--自己有水印，代理无水印：500275833  500275834
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId_channl,usercreateBean1.getNickname(),true);
			//提交发布商品
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(5000);
			//获取老的二维码
			LoadPicsBean loadpicsBean=new LoadPicsBean();
			loadpicsBean.setProductId(productId);
			loadpicsBean.setUserId(userId_channl);
			LoadPicsCall loadpicsCall=new LoadPicsCall();
			loadpicsCall.setData(loadpicsBean);
			loadpicsCall.callService();
			Logger.verifyEquals(200, loadpicsCall.getStatusCode(), "判断code");
			
			loadpicsBean.setUserId(userId_agent);
			loadpicsCall.setData(loadpicsBean);
			loadpicsCall.callService();
			Logger.verifyEquals(200, loadpicsCall.getStatusCode(), "判断code");
			String oldQrCodeAgent = loadpicsCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("pics").get(3).getAsString();

			//修改主图
			modifyagentproductBean = AddAgentProductCallService.initModifyAgentProductBean(productId,addAgentProductBean);
			modifyagentproductBean.getPics().set(0, "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/666666666.jpg");
			modifyagentproductCall.setData(modifyagentproductBean);
			modifyagentproductCall.callService();
			Logger.verifyEquals("200",modifyagentproductCall.getString("code"),"验证code");
			AgentProductCallService.checkAddAgentProduct(productId,addAgentProductBean,false);
			
			Thread.sleep(5000);
			//获取新的二维码
			loadpicsBean.setUserId(userId_channl);
			loadpicsCall.setData(loadpicsBean);
			loadpicsCall.callService();
			Logger.verifyEquals(200, loadpicsCall.getStatusCode(), "判断code");
			
			loadpicsBean.setUserId(userId_agent);
			loadpicsCall.setData(loadpicsBean);
			loadpicsCall.callService();
			Logger.verifyEquals(200, loadpicsCall.getStatusCode(), "判断code");
			String newQrCodeAgent = loadpicsCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("pics").get(3).getAsString();

			//代理新的二维码和旧的二维码不一致
			Logger.verifyNotEquals(newQrCodeAgent, oldQrCodeAgent, "新的和旧的二维码不一样--渠道商");
			
		} catch (Exception e) {
 		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_LoadPics_03() {
		Logger.start(true, "水印开关打开，继承水印关闭---上级有水印，下级无水印");
		try {
			int sellerId=500275657;
			int agentId=500275658;
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(500275657,"112233",true);
			//提交发布商品
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(5000);

			LoadPicsBean loadpicsBean=new LoadPicsBean();
			LoadPicsCall loadpicsCall=new LoadPicsCall();
			
			loadpicsBean.setProductId(productId);
			loadpicsBean.setUserId(500275657);
			loadpicsCall.setData(loadpicsBean);
			loadpicsCall.callService();
			Logger.verifyEquals(200, loadpicsCall.getStatusCode(), "判断code");
			JsonObject sellerjson = loadpicsCall.getJsonObject();
			
			loadpicsBean.setProductId(productId);
			loadpicsBean.setUserId(500275658);
			loadpicsCall.setData(loadpicsBean);
			loadpicsCall.callService();
			Logger.verifyEquals(200, loadpicsCall.getStatusCode(), "判断code");
			
			JsonObject agentjson = loadpicsCall.getJsonObject();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_LoadPics_04() {
		Logger.start(true, "水印开关打开，继承水印关闭---上级有水印，下级无水印");
		try {
			int userId_channl=500275675;
			int userId_agent=500275676;
				AddAgentProductBean sellerProductBean = AddAgentProductCallService.initAddAgentProduct(userId_channl,"666666",true);
				//提交发布商品
				String f1 = System.getProperty("user.dir") + File.separator
					    + "resource" + File.separator + "Penguins.jpg";
				FontImage.addWaterMark(f1, f1,Arrays.asList("测试"+TestDataManager.getRandomNum(6)),new Font("blod", 1, 20),Color.BLACK,"png");
				String picUrl= TestDataManager.UploadFile(f1);
				sellerProductBean.getPics().set(2, picUrl);
				String productId = AddAgentProductCallService.AddAgentProduct(sellerProductBean);
				Thread.sleep(5000);
				ImportAgentProductBean AgentProductBean = AddAgentProductCallService.initImportAgentProduct(userId_agent,"777777",productId,userId_channl,false);
			
				String productId1 = AddAgentProductCallService.ImportAgentProduct(AgentProductBean);
			
			Thread.sleep(5000);
			

			
			loadpicsBean.setProductId(productId);
			loadpicsBean.setUserId(userId_channl);
			loadpicsCall.setData(loadpicsBean);
			loadpicsCall.callService();
			Logger.verifyEquals(200, loadpicsCall.getStatusCode(), "判断code");
			JsonObject sellerjson = loadpicsCall.getJsonObject();
			
			loadpicsBean.setProductId(productId1);
			loadpicsBean.setUserId(userId_agent);
			loadpicsCall.setData(loadpicsBean);
			loadpicsCall.callService();
			Logger.verifyEquals(200, loadpicsCall.getStatusCode(), "判断code");
			
			JsonObject agentjson = loadpicsCall.getJsonObject();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
}